package com.google.code.p.ivans;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.p.ivans.entities.Order;
import com.google.code.p.ivans.entities.Waiter;
import com.google.code.p.ivans.myexceptions.EntityNotFoundException;
import com.google.code.p.ivans.myexceptions.NoLinksException;
import com.google.code.p.ivans.serviceinterfaces.WaiterServiceInterface;
import com.google.code.p.ivans.serviceinterfacesimpls.WaiterServiceInterfaceImpl;


public class EditWaiter extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		WaiterServiceInterface waiterSI = new WaiterServiceInterfaceImpl();
		
		try {
			Waiter waiter = waiterSI.getByID(Integer.valueOf(request.getParameter("id")));
			request.setAttribute("waiter", waiter);
			
			List<Order> orders = waiterSI.getWaitersOrders(waiter.getId());		
			
			request.setAttribute("orders", orders);
			
		} catch (NumberFormatException e) {
			request.setAttribute("error", "NumberFormatExc");
		
			
		} catch (EntityNotFoundException e) {
			request.setAttribute("error", "EntityNotFound");			
			
		} catch (SQLException e) {			
			request.setAttribute("error", e.getMessage());			
			
		} catch (NoLinksException e) {			
			request.setAttribute("error", "NoLink");
			
		}
		
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("editWaiter.jsp");
		
		requestDispatcher.forward(request, response);
	}

}
